//
//  HRGoodsListViewController.m
//  haierapp
//
//  Created by zhang on 14-8-7.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRGoodsListViewController.h"

#import "HRGoodsListCell.h"

#import "HRGoodsInfoViewController.h"

#import "HRGoodsSearchViewController.h"

#import "HRProductList.h"
#import "HRProListItem.h"

#define PAGE_SIZE_IN_GOODSLIST 20

@interface HRGoodsListViewController (){
    UITextField *_searchTextField;
    
    ORDER_TYPE_PRODUCT_LIST _orderType;
    
    int _pageSize;
    int _PageNum;
}

@property(nonatomic,strong) NSArray *dataArray;

@property (weak, nonatomic) IBOutlet UITableView *mytableview;
@property (weak, nonatomic) IBOutlet UISegmentedControl *mySegmentButton;

@end

@implementation HRGoodsListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *placeHolder;
    
    if([_searchContent isEqualToString:@""] || _searchContent==nil){
        placeHolder=@"请输入搜索词";
    }else{
        placeHolder=_searchContent;
    }
    
    
    
    //show search in navigation
    NavigationViewController *navigationController=(NavigationViewController *)self.navigationController;
    _searchTextField=[navigationController showGoodsSearch:self placeholder:placeHolder withSearchAction:@selector(onSeartButtonActive:)];
    
    
    _pageSize=PAGE_SIZE_IN_GOODSLIST;
    
    _PageNum=0;
    
    _orderType=ORDER_TYPE_PRODUCT_LIST_TIME;
    
    //加载商品列表数据
    [self loadGoodsList];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //显示顶层navigationBar
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - UISegment Change Action

- (IBAction)onSegmentChangeAction:(id)sender {
    
    UISegmentedControl *segmentControl=(UISegmentedControl *)sender;
    
    int selectedIndex=segmentControl.selectedSegmentIndex;
    
    if (selectedIndex==0) {
        //按时间排序
        _orderType=ORDER_TYPE_PRODUCT_LIST_TIME;
    }else if(selectedIndex==1){
        //按销量排序
        _orderType=ORDER_TYPE_PRODUCT_LIST_SALES;
    }else{
        //按好评排序
        _orderType=ORDER_TYPE_PRODUCT_LIST_PRAISE;
    }
    
    _PageNum=0; //重新加载数据
    
    //重新加载数据
    [self loadGoodsList];
    
}


#pragma mark - Search Goods List Methods

//加载商品数据
-(void)loadGoodsList{
    
    //隐藏为空提示页面
    [self hideHud];
    
    NSString *content;
    
    if (_type==TYPE_PRODUCT_LIST_BY_SEARCH) {
        //根据“关键字”搜索商品
        content=_searchContent;
    }else{
        //根据类别，获取商品
        content=_proCategoryId;
    }
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    NSString *pageSizeStr=[NSString stringWithFormat:@"%d",_pageSize];
    NSString *pageNumStr=[NSString stringWithFormat:@"%d",_PageNum];
    
    [HRInterfaceManager getProductListByType:_type withContent:content withPageSize:pageSizeStr withPageNumber:pageNumStr withOrderType:_orderType withOrderBy:2 completion:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            
            HRProductList *productList=(HRProductList *)data;
            
            _dataArray=productList.list;
            
            //页数加1
            _PageNum++;
            
            [self.mytableview reloadData];
            
            if (_dataArray==nil || [_dataArray count]==0) {
                //显示为空提示
                [self showHudByType:HUDTYPE_SEARCH];
            }
        }else{
            [self showTextHUD:message];
        }
        
        //页面加载耗时
        [self analysisEnd:@"商品列表"];

    }];
}


//根据关键字搜索商品
-(void)loadGoodsListBySearchKey:(NSString *)keyword{
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    [HRInterfaceManager getProductListBySearch:keyword withPageSize:_pageSize withPageNumber:_PageNum completion:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            
            HRProductList *productList=(HRProductList *)data;
            
            _dataArray=productList.list;
            
            //页数加1
            _PageNum++;
            
            [self.mytableview reloadData];
            
            if (_dataArray==nil || [_dataArray count]==0) {
                //显示为空提示
                [self showHudByType:HUDTYPE_SEARCH];
            }
        }else{
            [self showTextHUD:message];
        }
        
    }];
}

//通过类别加载产品列表
-(void)loadGoodsListByCategory:(NSString *)categoryId{
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    [HRInterfaceManager getProductListByCategoryId:categoryId withPageSize:_pageSize withPageNumber:_PageNum completion:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            HRProductList *productList=(HRProductList *)data;
            _dataArray=productList.list;
            
            //页数加1
            _PageNum++;
            
            [self.mytableview reloadData];
            
            if (_dataArray==nil || [_dataArray count]==0) {
                //显示为空提示
                [self showHudByType:HUDTYPE_SEARCH];
            }
        }else{
            [self showTextHUD:message];
        }
    }];
}


//点击导航条上的“搜索”按钮触发事件
-(void)onSeartButtonActive:(id)sender{
    
    if (_searchContent==nil || [_searchContent isEqualToString:@""]) {
        [self showGoodsSearchPage];
    }else{
        [self loadGoodsList];
    }
}

-(void)showGoodsSearchPage{
    HRGoodsSearchViewController *goodsSearchViewController=[[HRGoodsSearchViewController alloc]initWithStyle:UITableViewStylePlain];
    
    goodsSearchViewController.searchText=_searchContent;
    
    [self.navigationController pushViewController:goodsSearchViewController animated:YES];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = CELL_INDENTIFIER_GOODSLIST;
    HRGoodsListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell=[[HRGoodsListCell alloc] init];
    }
    
    [cell configWithData:[_dataArray objectAtIndex:indexPath.row]];
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT_GOODSLIST;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]init];
    return view;
}


 #pragma mark - Table view delegate
 
 // In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
     
     HRProListItem *productItem=(HRProListItem *)[_dataArray objectAtIndex:indexPath.row];
     
     HRGoodsInfoViewController *goodsInfoViewController=[[HRGoodsInfoViewController alloc] initWithNibName:NIBNAME_GOODSINFO bundle:nil];
     
     goodsInfoViewController.goodsId=[productItem.productId stringValue];
     
     [self.navigationController pushViewController:goodsInfoViewController animated:YES];
     
 }


#pragma mark - UITextField Delegate Methods

- (void)textFieldDidBeginEditing:(UITextField *)textField{
  
    [textField resignFirstResponder];
    
    [self showGoodsSearchPage];
}


@end
